package com.rst.model;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "t_USER_DETAILS")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class UserDetailsDTO extends CommonDetails implements UserDetails {

	private static final long serialVersionUID = 4252923265521334091L;
	public static final boolean ENABLE = true;
	

	private String username;
	private String firstName;
	private String middleName;
	private String lastName;

	private String emailId;
	private String password;
	private String reset_password_code;
	private Date reset_password_code_until;

	private boolean isAccountNonExpired;
	private boolean isAccountNonLocked;
	private boolean isCredentialsNonExpired;
	private boolean isEnabled;
	private boolean credentialsExpired;					//additional field for checking of credentials expiry

	private Integer count;
	private String[] role;
	private Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();


	public UserDetailsDTO(){}
	@Id
	@Column(name = "f_USER_ID", length = 20)
	public String getUsername() {
		return username;
	}
	

	@Column(name = "f_EMAIL_ID", length = 50)
	public String getEmailId() {
		return emailId;
	}

	@Basic
	@Column(name = "f_HASHED_PASSWORD", length = 500, nullable = false)
	public String getPassword() {
		return password;
	}

	@Column(name = "f_IS_ACNT_NON_EXPD", nullable = false, columnDefinition = "smallint default 1")
	public boolean isAccountNonExpired() {
		return isAccountNonExpired;
	}

	@Column(name = "f_IS_ACNT_NON_LOCKD", nullable = false, columnDefinition = "smallint default 1")
	public boolean isAccountNonLocked() {
		return isAccountNonLocked;
	}

	@Column(name = "f_IS_CRED_NON_EXPD", nullable = false, columnDefinition = "smallint default 1")
	public boolean isCredentialsNonExpired() {
		return isCredentialsNonExpired;
	}

	@Column(name = "f_IS_ENABLED", nullable = false, columnDefinition = "smallint default 1")
	public boolean isEnabled() {
		return isEnabled;
	}

	@ManyToMany(targetEntity = RoleDetails.class, cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
	@JoinTable(name = "t_USER_ROLE_MAPPING", joinColumns = @JoinColumn(name = "f_URM_USER_ID"), inverseJoinColumns = @JoinColumn(name = "f_URM_ROLE_ID"))
	public Collection<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Column(name = "f_COUNT", nullable = true)
	public Integer getCount() {
		return count;
	}
	
	@Column(name = "f_RESET_PASSWORD_CODE", length = 500)
	public String getReset_password_code() {
		return reset_password_code;
	}

	@Column(name="f_RESET_PASSWORD_CODE_UNTIL")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getReset_password_code_until() {
		return reset_password_code_until;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}

	public void setAccountNonExpired(boolean isAccountNonExpired) {
		this.isAccountNonExpired = isAccountNonExpired;
	}

	public void setAccountNonLocked(boolean isAccountNonLocked) {
		this.isAccountNonLocked = isAccountNonLocked;
	}

	public void setCredentialsNonExpired(boolean isCredentialsNonExpired) {
		this.isCredentialsNonExpired = isCredentialsNonExpired;
	}

	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}

	public void setCount(Integer count) {
		this.count = count;
	}



	public void setPassword(String password) {
		this.password = password;
	}

	public void setReset_password_code(String reset_password_code) {
		this.reset_password_code = reset_password_code;
	}

	public void setReset_password_code_until(Date reset_password_code_until) {
		this.reset_password_code_until = reset_password_code_until;
	}

		
	@Column(name = "f_USER_NAME_FIRST", length = 500)
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	@Column(name = "f_USER_NAME_MIDDLE", length = 500)
	public String getMiddleName() {
		return middleName;
	}
	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}
	@Column(name = "f_USER_NAME_LAST", length = 500)
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	@Transient
	public String[] getRole() {
		return role;
	}
	
	public void setRole(String[] role) {
		this.role = role;
	}
	
	@Transient
	public boolean isCredentialsExpired() {
		return credentialsExpired;
	}

	public void setCredentialsExpired(boolean credentialsExpired) {
		this.credentialsExpired = credentialsExpired;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "UserDetailsDTO [username=" + username
				+ ", firstName=" + firstName
				+ ", middleName=" + middleName
				+ ", lastName=" + lastName + ", emailId="
				+ emailId + ", isAccountNonExpired=" + isAccountNonExpired
				+ ", isAccountNonLocked=" + isAccountNonLocked
				+ ", isCredentialsNonExpired=" + isCredentialsNonExpired
				+ ", isEnabled=" + isEnabled + ", credentialsExpired="
				+ credentialsExpired + ", count=" + count + ", authorities="
				+ authorities + "]";
	}
}
